/**
 * 
 */
package org.ko.core.domain.auth;

import java.util.List;

import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.JoinColumn;
import javax.persistence.JoinTable;
import javax.persistence.ManyToMany;
import javax.persistence.Table;

import org.ko.core.domain.support.BaseEntity;



/**
 * @author yangfuming
 *
 * modify on 2014-9-22 by zengxm
 * ------------
 * ALTER TABLE `oa_role_permission` DROP FOREIGN KEY `oa_role_permission_ibfk_2`;
   ALTER TABLE `oa_role_permission` ADD CONSTRAINT `oa_role_permission_ibfk_2` FOREIGN KEY 
   			(`permission_id`) REFERENCES `oa_permission` (`id`) ON DELETE CASCADE ON UPDATE RESTRICT;
   -----------------
   由hibernate自动创建的关联表，无法满足一下需求：
   删除 Permission 时，不管有没有role 跟这个Permission 关联，都可以删除Permission， 而且与之关联的 role 不会被删除
 *原因是有外键的约束，目前没有找到用注解的方式定义外键约束，所以采用的方法是表创建后，人工修改外键约束，即运行上面的sql语句
 *
 *
 */
@Entity
@Table(name="SYS_ROLE")
public class Role extends BaseEntity{
	private static final long serialVersionUID = 2556338922618244333L;
	private String name;
    private String remarks;
    
    private List<Permission> permissions;
    
	@ManyToMany(cascade={CascadeType.REFRESH}, fetch = FetchType.LAZY)
	@JoinTable(name = "SYS_ROLE_PERMISSION", 
			joinColumns = { @JoinColumn(name = "role_id",updatable=true) }, 
			inverseJoinColumns = {
				@JoinColumn(name = "permission_id", 
				updatable = false
				)
			}
	)
	public List<Permission> getPermissions() {
		return permissions;
	}
	public void setPermissions(List<Permission> permissions) {
		this.permissions = permissions;
	}
	
	public Role(Long id) {
		this.setId(id);
	}
    public Role() {
		
	}
	@Column(name = "name",nullable=false,length=32)
	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}
	@Column(name = "remarks",length=255)
	public String getRemarks() {
		return remarks;
	}
	public void setRemarks(String remarks) {
		this.remarks = remarks;
	}
	
}
